import app from '../../index';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let couponItem = JSON.parse(options.item);
    if (couponItem.validStartDate) {
      couponItem.validStartDate = this.formateTime(couponItem.validStartDate.slice(0, 10));
    }
    if (couponItem.validEndDate) {
      couponItem.validEndDate = this.formateTime(couponItem.validEndDate.slice(0, 10));
    }
    if (couponItem.couponAmount) {
      if (couponItem.couponAmount.toString().indexOf('.') != -1) {
        couponItem.couponAmount = couponItem.couponAmount;
      } else {
        couponItem.couponAmount = couponItem.couponAmount + '.00';
      }

      if (couponItem.floorAmount.toString().indexOf('.') != -1) {
        couponItem.floorAmount = couponItem.floorAmount;
      } else {
        couponItem.floorAmount = couponItem.floorAmount + '.00';
      }
    }

    this.setData({
      coupon: couponItem
    });
  },

  /**
   * 查看可使用店铺
   */
  clickUseShop() {
    app.router('../canUseShopList/canUseShopList?couId=' + this.data.coupon.id);
  },

  /**
   * 替换字符串
   */
  formateTime(textstr) {
    var datetimes = textstr;
    if (datetimes == null || datetimes == '') {
      return;
    }
    if (textstr.indexOf('-') != -1) {
      datetimes = textstr.replace(/\-/g, '.');
    }
    return datetimes;
  }
})